﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;

namespace wUniApp.MVC.Helpers
{
  public static class wLangHelper
  {
    public static void Refresh()
    {
      var ses = HttpContext.Current.Session;
      if (ses["current_language"] == null)
      {
        ses["current_language"] = Thread.CurrentThread.CurrentUICulture.IetfLanguageTag;
      }

      string cl = ses["current_language"].ToString();
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cl);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(cl);
    }
    public static void SetLanguage(string langCode, string returnUrl)
    {
      SetLanguage(langCode);
      HttpContext.Current.Response.Redirect(returnUrl);
    }
    public static void SetLanguage(string langCode)
    {
      if (String.IsNullOrEmpty(langCode)) langCode = Thread.CurrentThread.CurrentUICulture.IetfLanguageTag; ;
      var ses = HttpContext.Current.Session;
      ses["current_language"] = langCode;
      Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(langCode);
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(langCode);
    }

    public static string GetLocText(string resName)
    {
      try
      {
        string res = wUniApp.MVC.App_GlobalResources.Resources.ResourceManager.GetString(resName);
        if (String.IsNullOrEmpty(res)) res = resName;
        return res;
      }
      catch
      {
        return resName;
      }
    }

  }
}